package cn.aitstone.pet.service.impl;

import cn.aitstone.basic.service.impl.BaseServiceImpl;
import cn.aitstone.basic.utils.BaiduAuditUtils;
import cn.aitstone.basic.utils.ImgCensor;
import cn.aitstone.pet.domain.Pet;
import cn.aitstone.pet.domain.PetDetail;
import cn.aitstone.pet.domain.PetOnlineAuditLog;
import cn.aitstone.pet.mapper.PetDetailMapper;
import cn.aitstone.pet.mapper.PetMapper;
import cn.aitstone.pet.mapper.PetOnlineAuditLogMapper;
import cn.aitstone.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sheldon
 * @since 2022-07-16
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
    * @Title: save
    * @Description: 新增宠物
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/16 17:04
    * @Parameters: [pet]
    * @Return void
    */
    @Override
    public void save(Pet pet) {
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.save(detail);

    }

    @Override
    public void onsale(List<Long> ids, Long eId) {
        for (Long id : ids){
            // 得到每个宠物基本信息对象
            Pet pet = petMapper.loadById(id);
            StringBuffer sb = new StringBuffer();
            // 调用百度AI进行文字，图片的审核
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append("宠物名称不合法，宠物名称为：" + pet.getName());
            }
            String[] arr = pet.getResources().split(",");
            for (String ar : arr){
                if (!ImgCensor.ImgCensor(ar)){
                    sb.append("宠物图片不合法，宠物图片路径：" + ar);
                }
            }
//            if (!ImgCensor.ImgCensor(pet.getResources().split(",")[0])){
//                sb.append("宠物图片不合法，宠物图片路径：" + pet.getResources().split(",")[0]);
//            }
            if (sb.length() > 0){
                // 如果违规，我们要保存审核失败日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setNote(sb.toString());
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setAuditId(eId);
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                continue;
            }
            petMapper.onsale(id);


        }
    }

    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    @Override
    public void update(Pet pet) {
        petMapper.update(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.update(detail);
    }
}
